import { ref, computed } from 'vue'
import { defineStore } from 'pinia'

export interface TaskStatus {
  id: string
  name: string
  status: 'pending' | 'running' | 'completed' | 'failed'
  progress: number
  startTime?: string
  endTime?: string
  device?: string
  type?: string
}

export const useTaskStore = defineStore('task', () => {
  const tasks = ref<TaskStatus[]>([
    {
      id: '001',
      name: 'Web登录流程测试',
      status: 'running',
      progress: 75,
      startTime: '2024-01-15 10:30',
      device: '服务器-001',
      type: '功能测试'
    },
    {
      id: '002',
      name: 'API接口性能测试',
      status: 'completed',
      progress: 100,
      startTime: '2024-01-15 09:15',
      endTime: '2024-01-15 09:45',
      device: '服务器-002',
      type: '性能测试'
    }
  ])

  const runningTasks = computed(() => 
    tasks.value.filter(task => task.status === 'running')
  )

  const completedTasks = computed(() =>
    tasks.value.filter(task => task.status === 'completed')
  )

  const failedTasks = computed(() =>
    tasks.value.filter(task => task.status === 'failed')
  )

  const totalTasks = computed(() => tasks.value.length)

  const addTask = (task: Omit<TaskStatus, 'id'>) => {
    const newTask: TaskStatus = {
      ...task,
      id: Date.now().toString()
    }
    tasks.value.push(newTask)
  }

  const updateTask = (id: string, updates: Partial<TaskStatus>) => {
    const index = tasks.value.findIndex(task => task.id === id)
    if (index !== -1) {
      tasks.value[index] = { ...tasks.value[index], ...updates }
    }
  }

  const removeTask = (id: string) => {
    const index = tasks.value.findIndex(task => task.id === id)
    if (index !== -1) {
      tasks.value.splice(index, 1)
    }
  }

  const getTaskById = (id: string) => {
    return tasks.value.find(task => task.id === id)
  }

  return {
    tasks,
    runningTasks,
    completedTasks,
    failedTasks,
    totalTasks,
    addTask,
    updateTask,
    removeTask,
    getTaskById
  }
})